package com.project.erp.interceptor;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
import com.project.erp.common.Constants;
import com.project.erp.common.utils.CookieUtil;
import com.project.erp.common.utils.I18nUtils;
import com.project.erp.common.utils.SpringContextHolder;
import com.project.erp.common.utils.encode.EncodeUtils;
import com.project.erp.model.system.Privilege;
import com.project.erp.model.system.User;
import com.project.erp.service.system.PrivilegeService;
import com.project.erp.service.system.UserService;

/**
 * 系统自动登陆处理
 * @author zyj
 *
 */
@SuppressWarnings("serial")
public class AutoLoginInterceptor implements Interceptor{

	protected final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void destroy() {
	}

	@Override
	public void init() {
	}

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		try {
			ActionContext ctx = invocation.getInvocationContext();
			HttpServletRequest request = ServletActionContext.getRequest();
			HttpServletResponse response = ServletActionContext.getResponse();
			User currentUser = (User) ctx.getSession().get(Constants.ERP_CURRENT_USER);
			//没有登陆情况
			if(null == currentUser){
				String cookieValue = CookieUtil.getCookieValue(request, Constants.ERP_COOKIE_CURRENT_KEY);
				if(StringUtils.isNotEmpty(cookieValue)){
					String cookieValueAfterDecode = new String(EncodeUtils.base64Decode(cookieValue));
					String cookieValues[] = cookieValueAfterDecode.split(Constants.WEB_COOKIE_SPLIT);
					//如果长度为3才处理自动登陆
					if(cookieValues.length == 3){
						 String loginUserName = cookieValues[1];
						 String password = cookieValues[2];
						 UserService userService = SpringContextHolder.getBean("userServiceImpl");
						 User loginUser = userService.getUserByUserName(loginUserName);
						 if(null != loginUser && loginUser.getPassword().equals(password)){
							 logger.debug("系统自动登陆操作...");
						 	String cookieValue2 = Constants.WEB_COOKIE_KEY + Constants.WEB_COOKIE_SPLIT + loginUser.getLoginName() + Constants.WEB_COOKIE_SPLIT + loginUser.getPassword();
						 	String cookieValueBase64 = EncodeUtils.base64Encode(cookieValue2.getBytes());
							CookieUtil.setCookie(request, response, Constants.ERP_COOKIE_CURRENT_KEY, cookieValueBase64, Constants.ERP_COOKIE_MAX_AGE);
							//存放用户信息
							request.getSession().setAttribute(Constants.ERP_CURRENT_USER, loginUser);
							PrivilegeService privilegeService = SpringContextHolder.getBean("privilegeServiceImpl");
							//根据角色获取所有的菜单信息
							List<Privilege> privilegeList = privilegeService.getPrivilegeByRoles(loginUser.getRoleList());
							//存放用户权限码信息
							request.getSession().setAttribute(Constants.ERP_CURRENT_USER_RIGHTCODES, privilegeList);
							
							I18nUtils.setDefaultLocale(request.getSession());
							return "autoLoin";
						 }
					}
				}
			}
		} catch (Exception e) {
			logger.debug("系统自动登陆操作异常.",e);
		}
        return invocation.invoke();  
	}

}
